package com.inei.shapefile_01;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;

public class MainActivity extends FragmentActivity {
	SimpleExample simple;
	TextView tvResultado;
	List<List<LatLng>> lista;
	GoogleMap mapa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tvResultado = (TextView) findViewById(R.id.tvResultado);
		mapa = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();
		mapa.setMyLocationEnabled(true);
		Cargar cargar = new Cargar();
		cargar.execute();
	}

	public class Cargar extends AsyncTask<Void, List<LatLng>, Void> {
		ProgressDialog progress;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			//progress = new ProgressDialog(getApplicationContext());
			//progress.setTitle("Cargando");
			//progress.setMessage("Se estan leyendo los archivos SHAPE...");
			// progress.
		}

		@Override
		protected void onProgressUpdate(List<LatLng>... values) {
			agregarPoligono(values[0]);
		}
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			File file = new File(Environment.getExternalStorageDirectory(),
					"POLYGONO_CCPP_QUINUA_41.shp");
			try {
				FileInputStream fis = new FileInputStream(file);
				simple = new SimpleExample();
				lista = simple.lista(fis);
				for (int i = 0; i < lista.size(); i++) {
					publishProgress(lista.get(i));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

	

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
		}
	}

	public void agregarPoligono(List<LatLng> puntos) {
		PolygonOptions options = new PolygonOptions();
		options.strokeWidth(1);
		for (LatLng p : puntos) {
			options.add(p);
		}
		
		options.fillColor(Color.HSVToColor(50, new float[] { 25, 1, 1 }));
		Polygon po = mapa.addPolygon(options);
		
		 Marker m = mapa.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(textAsBitmap("Punto",14,Color.BLACK))).position(puntos.get(0)).title("Punto").visible(true));

	}
	
	public Bitmap textAsBitmap(String text, float textSize, int textColor) {
	    Paint paint = new Paint();
	    paint.setTextSize(textSize);
	    paint.setColor(textColor);
	    paint.setTextAlign(Paint.Align.LEFT);
	    int width = (int) (paint.measureText(text) + 0.5f); // round
	    float baseline = (int) (-paint.ascent() + 0.5f); // ascent() is negative
	    int height = (int) (baseline + paint.descent() + 0.5f);
	    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
	    Canvas canvas = new Canvas(image);
	    canvas.drawText(text, 0, baseline, paint);
	    return image;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
